package com.whately.core.database.common.config;

import com.whately.core.database.common.constant.WhatelyDbConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mapper 拦截器
 * 设置数据源
 */
@Configuration
@Slf4j
public class WhatelyDao4SequenceAspect {

    /****************************************************************************
     * mapper dao 操作时，动态db检查
     * 只拦截 Sequence 的mapper
     * 其余的mapper 没有必要走拦截，因为事务是走拦截的
     ****************************************************************************/
    @Bean
    public DefaultPointcutAdvisor mapperPointcutAdvisor4CoreSequence() {
        //需要拦截的包的位置
        //拦截所有
        //String dao_point_cut_execution = "execution(* com.whately..dal.mapper.*.*(..))" ;

        //只需要拦截 Sequence的mapper ，其余mapper 不拦截会取默认default库 ， 其它特殊的辅助库，走另外一个拦截（事务已经拦截了，就没必要再拦截了）

        //拦截器
        SequenceDaoWhatelyInterceptor daoInterceptor = new SequenceDaoWhatelyInterceptor();
        //切点
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(WhatelyDbConstant.SEQ_DIC_MAPPER_POINT_CUT_EXECUTION);

        // 配置增强类advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(daoInterceptor);
        return advisor;
    }

}
